<?php
class StaticPagesController extends AppController {
	public $name = 'StaticPages';
	public $uses=array('StaticPage');
	public $helpers=array('Common');
	public $components=array('Common');
	function admin_index(){
		//get filter field
		$filterField=array(				
			$this->uses[0].'.keycode'=>_resource('keycode',true),
			$this->uses[0].'.title'=>_resource('title',true),	
		);
		$this->set('filterField',$filterField);
		//set list cac country name ra view
		if($this->webConfig['is_multi_lang'])
			$this->set('listLang',$this->Common->listCountry());
		//===================================================================
		$this->render('admin_index','ajax');
	}	
	
	function admin_lists(){
		//init session
		if(!$this->Session->check($this->uses[0])){
			$tmp=array(
				'limit'=>$this->webConfig['default_row_per_page'],
				'conditions'=>'',
				'fields'=>null,
				'order'=>null,
				'page'=>1,
				'recursive'=>0,
				'sortField'=>null,
				'sortDir'=>'asc',				
				'filterField'=>'',
				'filterText'=>'',
				'catid'=>'',
			);
			$this->Session->write($this->uses[0],$tmp);			
		}
		
		
		//get data
		$this->{$this->uses[0]}->locale=$this->Session->read('adminLang');
		$this->paginate=$this->Session->read($this->uses[0]);				
		$data0=$this->paginate($this->uses[0]);
		//su dung khi site co chuc nang da ngon ngu==========================
		//duyet qua tat ca cac rows trong $data0 de kiem tra xem data cua rows da co ngon ngu nao roi.
		if($this->webConfig['is_multi_lang']){
			foreach($data0 as $k=>$v){
				$data0[$k]['Langs']=$this->getLangs($v[$this->uses[0]]['id'],$this->uses[0]);
			}
			$this->set('listLang',$this->Common->listCountry());
		}
		//===================================================================
		$this->set('data0',$data0);
		$this->render('admin_lists');
	}
	
	function admin_add($locale=null){		
		if(!$locale){
			$locale=$this->Session->read('adminLang');
		}
		$this->{$this->uses[0]}->locale=$this->Session->read('adminLang');
		$isInsert=false;
		if(!empty($this->data)){
			if($this->{$this->uses[0]}->create($this->data) && $this->{$this->uses[0]}->validates()){				
					if($this->{$this->uses[0]}->save($this->data)) {
						$this->data=null;
						$isInsert=true;
						$this->Session->write('message',_resource('insert data successful',true));
					} else {
						$this->Session->write('error',_resource('can not insert data',true));
					}

			}else{
				$this->Session->write('error',_resource('red fill is require',true));
			}
		}
		if($this->webConfig['is_multi_lang'])
			$this->set('listLang',$this->Common->listCountry());
		$this->set('isInsert',$isInsert);
		$this->set('locale',$locale);
	}

	function admin_edit($id=null,$locale=null){	
		if(!$locale){
			$locale=$this->Session->read('adminLang');
		}
		$this->{$this->uses[0]}->locale=$locale;	
		$isInsert=false;
		if(!empty($this->data)){
			//t($this->data);
			if($this->{$this->uses[0]}->create($this->data) && $this->{$this->uses[0]}->validates()){
				
					if($this->{$this->uses[0]}->save($this->data)) {
						$this->data=null;
						$isInsert=true;
						$this->Session->write('message',_resource('update data successful',true));						
						
					} else {
						$this->Session->write('error',_resource('can not update data',true));
					}
				
			}else{
				$this->Session->write('error',_resource('red fill is require',true));
			}
		}else{
			if($id!=null){

				//get data from default language
				if($this->{$this->uses[0]}->locale!=$this->Session->read('adminLang')  && $this->webConfig['is_multi_lang']){
					$this->{$this->uses[0]}->locale=$this->Session->read('adminLang');
					$this->data =$this->{$this->uses[0]}->read(null,$id);
					$this->{$this->uses[0]}->locale=$locale;
					$data =$this->{$this->uses[0]}->read(null,$id);
					//get data of current select language
					foreach($this->{$this->uses[0]}->actsAs['Translate'] as $k=>$v){						
						$this->data[$this->uses[0]]['default'.$v]=$this->data[$this->uses[0]][$v];
						$this->data[$this->uses[0]][$v]=$data[$this->uses[0]][$v];
					}					
				}
				else{
					$this->data =$this->{$this->uses[0]}->read(null,$id);
				}
				
				if(empty($this->data)){	
					$this->Session->write('error',_resource('data not found',true));				
					$this->render('admin_editNotFound');
				}
			}
			else{				
				$this->Session->write('error',_resource('data not found',true));		
				$this->render('admin_edit_not_found');
			}
		}		
		$this->set('locale',$locale);
		if($this->webConfig['is_multi_lang'])
			$this->set('listLang',$this->Common->listCountry());
		$this->set('isInsert',$isInsert);
	}

	function admin_delete($id = null) {		
		$this->Session->delete('error');
		//if is submit:		
		if(isset($this->params['form']['check'])){
			$id_arr=$this->params['form']['check'];
		} else {
			$id_arr=null;
		}
		//if url
		if(intval($id)){
			$id_arr[$id]='';
		}
		if(count($id_arr)>0){
			//Xoa nhieu record
			foreach ($id_arr as $k=>$v){
				if(intval($k)>0){
					if(!$this->{$this->uses[0]}->delete($k)){
						$this->Session->write('error',_('error'));
					}
				}
			}
			if(!$this->Session->check('error')){
				$this->Session->write('message',_resource('delete data successful',true));
			}
		} else {
			$this->Session->write('error',_resource('no information to delete',true));
		}
		$this->admin_lists();		
	}
	
	function admin_changeLocale($locale=null){
		if($locale){
			$this->Session->write('adminLang',$locale);
		}
		$this->admin_lists();
	}
	
	
	function admin_published($id=null,$value=1,$field=0){
		$id_arr=null;
		$all=false;		
		switch($field){
			case 1: $fieldname='best';break;
			case 2: $fieldname='bestbuy';break;
			default: $fieldname='published';
		}
		if((!intval($value) && $value!=0) || intval($value)>1){
			$value=1;
		}
		if(isset($this->params['form']['check'])) {
			$id_arr=$this->params['form']['check'];
			$all=true;
		}
		
		//if url
		if(intval($id) && !$all){
			$id_arr[$id]='';
		}
		
		if(count($id_arr)>0){			
			$this->{$this->uses[0]}->query('update '.$this->{$this->uses[0]}->tablePrefix.$this->{$this->uses[0]}->table.' set '.$fieldname.'='.$value.' where id in ('.implode(',',array_keys($id_arr)).')' );
		}
		if (!$all) {			
			$this->set('value',$value);
			$this->set('field',$field);
			$this->set('fieldname',$fieldname);
			$this->set('id',intval($id));
			$this->render('admin_published','ajax');		
		} else {
			$this->admin_lists();
		}
		
	}
	
	function admin_sort($sortField=null,$sortDir='asc'){
		if($sortField){
			if($sortDir!='desc') $sortDir='asc';
			$this->Session->write($this->uses[0].'.sortField',$sortField);
			$this->Session->write($this->uses[0].'.sortDir',$sortDir);
			$this->Session->write($this->uses[0].'.order',$sortField.' '.$sortDir);
		}
		$this->admin_lists();
	}
	
	function admin_page($page=1){
		$this->Session->write($this->uses[0].'.page',$page);
		$this->admin_lists();
	}
	
	function admin_display($limit=0){
		if(intval($limit)){
			$this->Session->write($this->uses[0].'.limit',$limit);
		}
		$this->admin_lists();
	}

	function admin_filter(){
		if($this->request->is('post')){
			$this->Session->write($this->uses[0].'.filterText',$this->request->data['filter_text']);
			$this->Session->write($this->uses[0].'.filterField',$this->request->data['filter_field']);
			//$conditions=$this->Session->read($this->uses[0].'.conditions');
			if(!empty($this->request->data['filter_text'])){				
				$conditions[$this->request->data['filter_field'].' like']='%'.$this->request->data['filter_text'].'%';				
			}
			else{
				unset($conditions[$this->request->data['filter_field'].' like']);
			}
			
			$this->Session->write($this->uses[0].'.conditions',$conditions);
		}
		$this->admin_lists();
	}
	
	function admin_update(){
		if(isset($this->params['form']['order'])){
			foreach($this->params['form']['order'] as $k=>$v){
				if(intval($v)){					
					$this->{$this->uses[0]}->query('update '.$this->{$this->uses[0]}->table.' set `view_order`='.intval($v).' where id='.$k);
				}
			}
		}
		$this->Session->write('message',_resource('view order has been update',true));
		$this->admin_lists();
	}
	//===================================================================================
	
	function getContact(){
		$data=null;
		$this->{$this->uses[0]}->locale=$this->Session->read('siteLang');
		$data=$this->{$this->uses[0]}->findByKeycode('contact');
		return $data;
	}
	
	function getContent($content){
		$data=null;
		$this->{$this->uses[0]}->locale=$this->Session->read('siteLang');
		App::uses('Sanitize', 'Utility');
		$data=$this->{$this->uses[0]}->findByKeycode(Sanitize::clean($content));
		if(isset($data['StaticPage']['content']))$data=$data['StaticPage']['content'];
		return $data;
	}
}
?>
